﻿Public Class Confirmation
    Inherits System.Web.UI.Page
    Dim Flight_Number As Integer
    Dim User_Id As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If txtFlightNumber.Value = "" Then
            txtFlightNumber.Value = Request("ctl00$MainContent$txtFlightNumber")

        End If

        If txtUserId.Value = "" Then
            txtUserId.Value = Request("ctl00$MainContent$txtUserId")

        End If

        Flight_Number = Convert.ToInt32(txtFlightNumber.Value)
        User_Id = Convert.ToInt32(txtUserId.Value)
        FillData()
    End Sub

    '*** Gets a DataSet from the routes table ***'
    Function CreateDataSet(srtSql As String) As DataSet
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand
        Dim dtAdapter As New MySql.Data.MySqlClient.MySqlDataAdapter

        Dim ds As New DataSet
        'Dim i As Integer'
        Dim strConnString

        strConnString = "Server=localhost;User Id=goldair_dapp; Password=geckodev; Database=goldair_db; Pooling=false"

        objConn.ConnectionString = strConnString
        With objCmd
            .Connection = objConn
            .CommandText = srtSql
            .CommandType = CommandType.Text
        End With

        dtAdapter.SelectCommand = objCmd

        Try
            dtAdapter.Fill(ds, "user_flight")
        Catch ex As Exception

        End Try

        dtAdapter = Nothing
        objConn.Close()
        objConn = Nothing
        'Return DataSet'
        Return ds
    End Function

    Sub FillData()
        '*** DataSet From DataSet ***'
        Dim ds As DataSet
        'data = ("SELECT user_flight.user_id, user_flight.payment_date, user.first_name, user.last_name, route.destination, flight.flight_time, flight.arrive_time, user_flight.num_seat, flight.id, flight.cost FROM user_flight join flight on user_flight.flight_id = flight.id join user on user_flight.user_id = user.id join route on flight.route_id = route.id where user_id = 156681911 And flight_id = 1")

        ds = CreateDataSet("SELECT user_flight.user_id, user_flight.payment_date, user.first_name, user.last_name, route.destination, flight.flight_time, flight.arrive_time, user_flight.num_seat, flight.id, flight.cost FROM user_flight join flight on user_flight.flight_id = flight.id join user on user_flight.user_id = user.id join route on flight.route_id = route.id where user_id =" & User_Id & " And flight_id =" & Flight_Number)

        bookingNum.Text = ds.Tables(0).Rows(0)(0).ToString()
        bookingDate.Text = ds.Tables(0).Rows(0)(1).ToString()
        firstName.Text = ds.Tables(0).Rows(0)(2).ToString()
        lastName.Text = ds.Tables(0).Rows(0)(3).ToString()
        destination.Text = ds.Tables(0).Rows(0)(4).ToString()
        flightDate.Text = ds.Tables(0).Rows(0)(5).ToString()
        arriveDate.Text = ds.Tables(0).Rows(0)(6).ToString()
        setNum.Text = ds.Tables(0).Rows(0)(7).ToString()
        flightNum.Text = ds.Tables(0).Rows(0)(8).ToString()
        totalAmount.Text = ds.Tables(0).Rows(0)(9).ToString()

    End Sub

    Protected Sub btnConfirm_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnConfirm.Click
        ExecuteQuery("UPDATE user_flight SET confirmed=1 WHERE flight_id =" & Flight_Number & " AND user_id=" & User_Id & "")

        Dim strScript As String = "<script language=JavaScript>"
        strScript += "alert(""Thanks you for choosing Gold Air!"");window.location = ""Default.aspx"";"
        strScript += "</script>"
        If (Not ClientScript.IsStartupScriptRegistered("clientScript")) Then
            ClientScript.RegisterClientScriptBlock(Me.GetType(), "clientScript", strScript)
        End If




    End Sub
    Sub ExecuteQuery(ByVal srtSql As String)
        Dim objConn As New MySql.Data.MySqlClient.MySqlConnection
        Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand
        Dim dtAdapter As New MySql.Data.MySqlClient.MySqlDataAdapter

        Dim ds As New DataSet

        Dim strConnString

        strConnString = "Server=localhost;User Id=goldair_dapp; Password=geckodev; Database=goldair_db; Pooling=false"

        objConn.ConnectionString = strConnString
        objConn.Open()

        With objCmd
            .Connection = objConn
            .CommandText = srtSql
            .CommandType = CommandType.Text
        End With

        objCmd.ExecuteNonQuery()

        objConn.Close()
        objConn = Nothing



    End Sub
End Class